package java_test.io;

import java.io.*;

public class InputStreamReaderOutStreamWriterTest {
    public static void main(String[] args) {
        inputStreamReaderOutStreamWriterTest();
    }

    public static void inputStreamReaderTest() {
        InputStreamReader isr = null;

        try {
            isr = new InputStreamReader(new FileInputStream(new File("file\\text\\wordCount.txt")));

            char[] cbuf = new char[20];
            int len;

            while ((len = isr.read(cbuf)) != -1) {
                String str = new String(cbuf, 0, len);
                System.out.print(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void inputStreamReaderOutStreamWriterTest() {
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;

        try {
            isr = new InputStreamReader(new FileInputStream(new File("file\\text\\text1.txt")), "UTF-8");
            osw = new OutputStreamWriter(new FileOutputStream(new File("file\\text\\text2.txt")), "GBK");

            char[] cbuf = new char[20];
            int len;
            while ((len = isr.read(cbuf)) != -1) {
                osw.write(cbuf, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
